<?php
/**
  * 文章控制器
  */
class ArticleAction extends CommonAction{

	private $category=null;
	private $tree=null;

	public function _initialize()
	{
		$this->currentModel = D(MODULE_NAME);
		parent::_initialize();
		import("@.ORG.Tree");
		import("@.ORG.Tags");
		$this->category=D('Category');
		$this->tree=new Tree();
		$this->assign('flagArray',C('FLAG'));
	}

	public function index()
	{		
		$map=$this->_search();
		$this->_filter($map);
		$count=$this->currentModel->where($map)->count();
		$page=$this->_page($count,C('PAGE_SUM'));
		$datas=$this->currentModel->where($map)->limit($page->firstRow.','.$page->listRows)->order($this->pkId.' DESC')->select();
		$this->tree->init($this->category->getAllCategory('article'));
		$optionStr=$this->tree->getTreeOptions(0,"<option value=\$id \$selected>\$spacer\$title</option>","<optgroup label='\$spacer\$title'></optgroup>",$map['category_id']);
        $this->assign('optionStr',$optionStr);
		$this->assign('page',$page->show());
		$this->assign('datas',$datas);
		$this->display();
	}
	
	/**
	  * 过滤搜索条件
	  * @access private
	  * @param array &$map
	  */
	private function _filter(&$map)
	{
		$this->assign('param',$map);
        if(isset($map['title']))
        {
        	$map['title']=array('like',"%".$map['title']."%");
        }
        if((isset($_REQUEST['start_time'])&&$_REQUEST['start_time']>0)&&(isset($_REQUEST['end_time'])&&$_REQUEST['end_time']>0))
        {
        	$map['edit_time']=array('BETWEEN',strtotime($_REQUEST['start_time']).','.strtotime($_REQUEST['end_time']));
        }elseif(isset($_REQUEST['start_time'])&&$_REQUEST['start_time']>0)
        {
        	$map['edit_time']=array('EGT',strtotime($_REQUEST['start_time']));
        }elseif(isset($_REQUEST['end_time'])&&$_REQUEST['end_time']>0)
        {
        	$map['edit_time']=array('ELT',strtotime($_REQUEST['end_time']));
        }       
	}

	public function add()
	{
		if($this->isPost())
		{
			$this->_save();
		}else
			{
				$this->tree->init($this->category->getAllCategory('article'));
				$optionStr=$this->tree->getTreeOptions(0,"<option value=\$id \$selected>\$spacer\$title</option>","<optgroup label='\$spacer\$title'></optgroup>",0);
          		$this->assign('optionStr',$optionStr);
				$this->display();
			}
	}

	public function edit()
	{
		if($this->isPost())
		{
			$this->_save();
		}else
			{
				$id=$this->_get($this->pkId);
				$data=$this->currentModel->find($id);
				if(empty($data))$this->error('参数违法',U(MODULE_NAME.'/index'));
				$this->tree->init($this->category->getAllCategory('article'));
				$optionStr=$this->tree->getTreeOptions(0,"<option value=\$id \$selected>\$spacer\$title</option>","<optgroup label='\$spacer\$title'></optgroup>",$data['category_id']);
          		$this->assign('optionStr',$optionStr);
          		$this->assign('tags',$this->_getTagArticle($data['id']));			
				$this->assign('data',$data);
				$this->display('add');
			}
	}

	/**
	* 删除数据
	* @access public
	*/
	public function del()
	{
		$id=$this->_request($this->pkId);
		if(is_array($id))$id=implode(',',$id);
		if($this->currentModel->where()->delete($id)>0)
		{
			$this->_delTagArticle($id);
			$this->_delArticleComment('Article',$id);
			$this->success('成功',U(MODULE_NAME.'/index'));
		}else
			{
				$this->error('失败',U(MODULE_NAME.'/index'));
			}
	}

	/**
	* 改变信息状态
	* @access public
	*/
	public function change()
	{
		$id=$this->_post($this->pkId);
		if(empty($id))$this->error('请选择操作信息',U(MODULE_NAME.'/index'));
		$funcName=$this->_get('type');
		if(method_exists($this->currentModel,$funcName))
		{
			foreach($id as $v)
			{
				$this->currentModel->$funcName(array('id'=>$v));
			}
		}else{$this->error('非法操作',U(MODULE_NAME.'/index'));}
		$this->success('成功',U(MODULE_NAME.'/index'));
	}

    /**
      * 插入数据
      * @access private
      */
    private function _save()
    {
    	$this->_uploadThumb();
    	if($this->currentModel->create()===FALSE)
    	{
    		$this->error($this->currentModel->getError());
    	}
      	$id=$this->_post($this->pkId);
      	$funcName=$id>0?'save':'add';
      	if($this->currentModel->$funcName() !== false)
      	{
      		$id=$id>0?$id:$this->currentModel->getLastInsID();
      		$this->_insertTag($id);
      		$this->success('成功',U(MODULE_NAME.'/index'));
      	}else
      		{
      			@unlink(C('MY_UPLOAD_PATH').'thumb/'.$_POST['thumb']);
      			$this->error('失败',U(MODULE_NAME.'/index'));
      		}    	
    }

    /**
    * 插入关联标签
    * @access private
    * @param int $articleId
    **/
    private function _insertTag($articleId)
    {
    	$tagStr=$this->_post('tags');
    	$tagArr=explode(',',$tagStr);
    	$tag=new Tags($articleId);
    	$tag->add($tagArr);
    }

    /**
    * 删除评论
  	* @access private 
  	* @param string $source 来源
  	* @param int $articleId 关联信息ID
    */
    private function _delArticleComment($source,$articleId)
    {
    	$comment=M('Comment');
    	$where['source']=$source;
    	$where['value_id']=array('in',$articleId);
    	$comment->where($where)->delete();
    }    

    /**
    * 删除关联标签
  	* @access private 
  	* @param int $articleId 关联信息ID
    */
    private function _delTagArticle($articleId)
    {
    	Tags::delFromTagArticleByValueId($articleId);
    }

    /**
    * 获取关联标签
  	* @access private 
  	* @param int $articleId 关联信息ID
  	* @return string
    */
    private function _getTagArticle($articleId)
    {
    	$datas=Tags::getTagByArticleId($articleId);
    	if(empty($datas))
    	{
    		return '';
    	}else
    		{
    			foreach($datas as $data)
    			{
    				$str.=','.$data['title'];
    			}
    			return substr($str, 1);   			
    		}

    }    
   	
} 
?>